﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class ListProductHaveSent : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProduct();
            }
        }

        private void BindProduct()
        {
            ApplicationProductService service = new ApplicationProductService();
            IQueryable<ApplicationProduct> products;
            if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.Administrator ||
                ContextService.CurrentAccount.UserType.ID == (int)RoleType.TBGLY)
            {
                products = service.GetApplicationProductHasSent();
            }
            else
            {
                if (!ContextService.CurrentAccount.ServiceStationReference.IsLoaded)
                {
                    ContextService.CurrentAccount.ServiceStationReference.Load();
                }
                products = service.GetApplicationProductHasSent(ContextService.CurrentAccount.ServiceStation.ID);
            }
            RecordsNumber = products.Count();
            applicationGridView.DataSource = products;
            applicationGridView.DataBind();
        }

        protected void applicationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ApplicationProduct product = e.Row.DataItem as ApplicationProduct;
                Image image1 = e.Row.FindControl("Image1") as Image;
                if (product.ClientReceiveDateTime.HasValue && product.ClientReceiveDateTime.Value != DateTime.MinValue)
                {
                    image1.ImageUrl = "~/image/operateicon/right.gif";
                }
                //((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= applicationGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (applicationGridView.PageIndex + 1).ToString();
            }
        }

        protected void applicationGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < applicationGridView.PageCount)
            {
                applicationGridView.PageIndex = e.NewPageIndex;
            }
            
            BindProduct();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string pid = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("ConfirmProductReceive.aspx?{0}={1}", Constants.QUERYSTR_PRODUCT_ID, pid));
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<ApplicationProduct> dds = new List<ApplicationProduct>();
            dds.Add(new ApplicationProduct());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            applicationGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindProduct();
        }

        protected void applicationGridView_DataBound(object sender, EventArgs e)
        {
            if (applicationGridView.BottomPagerRow != null)
            {
                applicationGridView.BottomPagerRow.Visible = true;
            }
        }
    }
}
